﻿using System.Web.Mvc;
using BLL.Security;
using BLL.Services.Books;
using BLL.Services.Users;
using MvcContrib.UI.Grid;
using Web.Infrastructure;
using Web.Models;
using Web.Models.Account;

namespace Web.Controllers
{
    public partial class ProfileController : FrontendController
    {
        private readonly IUsersService _usersService;

        private readonly IEnvironmentContext _environmentContext;

        private readonly IBookService _bookService;

        public ProfileController(
            IUsersService usersService, IEnvironmentContext environmentContext, IBookService bookService)
        {
            _usersService = usersService;
            _environmentContext = environmentContext;
            _bookService = bookService;
        }

        public virtual ActionResult Show(string login)
        {
            var profileModel = new ShowProfileModel();
            profileModel += _usersService.GetUserByEmailOrLogin(login);

            var currentUser = _environmentContext.User;
            if (currentUser != null)
            {
                profileModel.CurrentUserId = currentUser.UserId;
            }
            return BackBoneJson(profileModel);
        }

        public virtual ActionResult SocialShow(string login)
        {
            var user = _usersService.GetUserByEmailOrLogin(login);
            return BackBoneJson(user);
        }

        public virtual ActionResult ProfileTemplate()
        {
            return BackBoneView(MVC.Profile.Views.ProfileTemplate);
        }

        [ListingAction("Name")]
        public virtual ActionResult RatedBooks(
            string login,
            int page,
            int size,
            GridSortOptions sort,
            CategoryBookModel.CategoryBookViewType? viewType)
        {
            if (string.IsNullOrEmpty(login))
                return BackBoneJson(string.Empty);
            var currentUser = _environmentContext.User;
            var userId = 0;
            if (currentUser != null)
                userId = currentUser.UserId;
            var books = _bookService.GetProfileRatedBooks(
                login, sort.Column, (int)sort.Direction, userId, page);
            var model = new CategoryBookModel(books);
            return BackBoneJson(model.GetData(viewType ?? CategoryBookModel.CategoryBookViewType.List));
        }

        public virtual ActionResult RatedBooksTemplate()
        {
            return BackBoneView(MVC.Profile.Views.RatedBooksTemplate);
        }

        public virtual ActionResult ListingIconViewTemplate()
        {
            return BackBoneView(MVC.Profile.Views.ListingIconViewTemplate);
        }

        public virtual ActionResult ListingListViewTemplate()
        {
            return BackBoneView(MVC.Profile.Views.ListingListViewTemplate);
        }

        public virtual ActionResult SocialProfileTemplate()
        {
            return BackBoneView(MVC.Profile.Views.SocialProfileTemplate);
        }
    }
}
